package com.kiki.favorite.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.kiki.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.kiki.favorite.mapper.FavoriteMapper;
import com.kiki.favorite.domain.Favorite;
import com.kiki.favorite.service.IFavoriteService;

/**
 * 用户收藏Service业务层处理
 * 
 * @author 林志毅
 * @date 2025-01-08
 */
@Service
public class FavoriteServiceImpl implements IFavoriteService 
{
    @Autowired
    private FavoriteMapper favoriteMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询用户收藏
     * 
     * @param favoriteId 用户收藏主键
     * @return 用户收藏
     */
    @Override
    public Favorite selectFavoriteByFavoriteId(Long favoriteId)
    {
        return favoriteMapper.selectFavoriteByFavoriteId(favoriteId);
    }

    /**
     * 查询用户收藏列表
     * 
     * @param favorite 用户收藏
     * @return 用户收藏
     */
    @Override
    public List<Favorite> selectFavoriteList(Favorite favorite)
    {
        return favoriteMapper.selectFavoriteList(favorite);
    }

    /**
     * 新增用户收藏
     * 
     * @param favorite 用户收藏
     * @return 结果
     */
    @Override
    public int insertFavorite(Favorite favorite) {
        System.out.println("添加收藏: " + favorite);

        // 查询是否存在在数据库里
        Boolean isExist = favoriteMapper.getExistFavorite(favorite);
        System.out.println("获取到的收藏: " + isExist);

        if (isExist) {
            // 如果存在，则删除 Redis 中的数据
            String redisKey = "user:" + favorite.getUserId() + ":favorite:" + favorite.getSongId();
            redisTemplate.delete(redisKey);
            return 0;
        } else {
            String userPrefix = "user:"; // 必须要有，不然没有目录效果
            String userPrefix2 = favorite.getUserId() + ":";
            String userPrefix3 = "favorite";
            String redisKey = userPrefix + userPrefix2 + userPrefix3; // 使用用户ID作为队列的键

            redisTemplate.opsForList().rightPush(redisKey, favorite.getSongId());
            // 插入数据库
            return favoriteMapper.insertFavorite(favorite);
        }
    }


    /**
     * 修改用户收藏
     * 
     * @param favorite 用户收藏
     * @return 结果
     */
    @Override
    public int updateFavorite(Favorite favorite)
    {
        return favoriteMapper.updateFavorite(favorite);
    }

    /**
     * 批量删除用户收藏
     * 
     * @param favoriteIds 需要删除的用户收藏主键
     * @return 结果
     */
    @Override
    public int deleteFavoriteByFavoriteIds(Long[] favoriteIds)
    {
        return favoriteMapper.deleteFavoriteByFavoriteIds(favoriteIds);
    }

    /**
     * 删除用户收藏信息
     * 
     * @param favoriteId 用户收藏主键
     * @return 结果
     */
    @Override
    public int deleteFavoriteByFavoriteId(Long favoriteId)
    {
        return favoriteMapper.deleteFavoriteByFavoriteId(favoriteId);
    }

    @Override
    public int delFavorite(Favorite favorite) {
        return favoriteMapper.deleteFavorite2(favorite);
    }

    @Override
    public int getFavoriteStatus(Favorite favorite) {
        return favoriteMapper.getFavoriteStatus(favorite);
    }
}
